<?php
//房桌管理
namespace App\Http\Controllers\App;
use App\Http\Controllers\App\BaseController as Controller;
use App\Models\Shop;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Models\ShopRoomTable;
use Illuminate\Http\Request;
use App\Repositories\RoomTableRepository;
class ManagementRoomTable extends Controller
{
    protected $thisLoginClerk;
    public function __construct(RoomTableRepository $roomTableRepository,Request $request)
    {
        $this->roomTableRepository=$roomTableRepository;
        $checkThisClerk = $this->getShopUserID($request->token,$request->device_id);
        if(!$checkThisClerk){
            return show(2001,'无效token');
        }else{//判断用户角色
            switch($checkThisClerk->role){        //权限处理
                case -1:    // 商家
                case 2:     // 经理
                case 1:     // 服务员
                    $this->thisLoginClerk = $checkThisClerk;
                    break;
                case 0:     //财务
                case 3:     // 主管
                    return show(2002,'没有权限');
                    break;
                default:
                    return show(2002,'没有权限');
                    break;
            }
        }
    }
    //新建房桌
    public function createRoomTable(Request $request)
    {
        $rules = [
            'shop_id' => 'required|integer|exists:shops,id',
            'type' => 'required|in:0,1',
            'hold'=>'required|integer',
            'name'=>'required|string',
            'min_consumption'=>'required',
            'lock_qrcode'=>'required|integer|in:0,1',
            'thumb'=>'required|string',
            'note'=>''
        ];
        $message = [
            'shop_id.*'=>'商家信息有误',
            'type.*'=>'请选择正确的方桌类型，记得勾选',     //0桌子1房间
            'hold.*'=>'请填写正确的容纳人数',
            'name.*'=>'请填写房间/桌子名称',
            'min_consumption.*'=>'请填写最低消费',
            'lock_qrcode.*'=>'请选择是否启用二维码',      // 是否启用二维码(0否，1是)
            'thumb.*'=>'请上传图片',
            'note.*'=>'请认真填写说明'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        try{
            $note = $request->note;
            if ($note){
                $note = $note.'';
            }
            $data = [
                'shop_id'=>$request->shop_id,
                'type'=>$request->type,
                'hold'=>$request->hold,
                'name'=>$request->name,
                'min_consumption'=>$request->min_consumption,
                'lock_qrcode'=>$request->lock_qrcode,
                'thumb'=>$request->thumb,
                'note'=>$note
            ];

            $now_role = $this->thisLoginClerk->role;
            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($request->shop_id,$user_id)){
                    return show(2002,'没有权限a');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $request->shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限b');
                }
            }
            //插入/更新
            shopRoomTable::create($data);
            return show(200,'创建房桌成功');
        }catch (\Exception $exception){
            return show(201,'创建房桌失败');
        }
    }
    // 修改房桌
    public function updateRoomTable(Request $request)
    {
        $rules = [
            'id' => 'required|integer|exists:shop_room_tables,id',
            'shop_id' => 'required|integer|exists:shops,id',
            'type' => 'required|in:0,1',
            'hold'=>'required|integer',
            'name'=>'required|string',
            'min_consumption'=>'required',
            'lock_qrcode'=>'required|integer|in:0,1',
            'thumb'=>'required|string',
            'note'=>''
        ];
        $message = [
            'id.*'=>'请选择正确的房桌',
            'shop_id.*'=>'商家信息有误',
            'type.*'=>'请选择正确的方桌类型，记得勾选',     //0桌子1房间
            'hold.*'=>'请填写正确的容纳人数',
            'name.*'=>'请填写房间/桌子名称',
            'min_consumption.*'=>'请填写最低消费',
            'lock_qrcode.*'=>'请选择是否启用二维码',      // 是否启用二维码(0否，1是)
            'thumb.*'=>'请上传图片',
            'note.*'=>'请认真填写说明'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        try{
            $now_role = $this->thisLoginClerk->role;
            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($request->shop_id,$user_id)){
                    return show(2002,'没有权限');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $request->shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限');
                }

            }
            $is_yours = ShopRoomTable::where('shop_id',$request->shop_id)->where('id',$request->id)->count();
            if (!$is_yours){
                return show(2002,'没有权限');
            }
            $note = $request->note;
            if ($note) {
                $note = $note . '';
            }
//            //插入/更新
            $data = [
                'type' =>$request->type,
                'name'=>$request->name,
                'hold'=>$request->hold,
                'min_consumption'=>$request->min_consumption,
                'lock_qrcode'=>$request->lock_qrcode,
                'thumb'=>$request->thumb,
                'note'=>$note
            ];

            DB::table('shop_room_tables')->where('id',$request->id)->update($data);
            return show(200,'修改房桌成功');
        }catch (\Exception $exception){
            return show(201,'修改房桌失败');
        }
    }

    //删除房桌
    public function deleteRoomTable(Request $request)
    {
        $rules = [
            'id' => 'required|integer|exists:shop_room_tables,id',
        ];
        $message = [
            'id.*'=>'房桌信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        try{
            $id = $request->id;

            $ShopRoomTable = ShopRoomTable::where('id',$id)->first();
            if ($ShopRoomTable){
                $shop_id = $ShopRoomTable->shop_id;
            }else{
                return show(2002,'没有权限');
            }
            $now_role = $this->thisLoginClerk->role;

            if ($now_role == '-1'){         // 商家
                // 判断这个商铺是否属于该商家
                $user_id = $this->thisLoginClerk->id;
                if (!Shop::checkIsShop($shop_id,$user_id)){
                    return show(2002,'没有权限');
                }
            }else{// 服务员
                // 判断这个商铺是否属于当前服务员的商铺
                if( $shop_id != $this->thisLoginClerk->shop_id){
                    return show(2002,'没有权限');
                }

            }

            if($ShopRoomTable->is_use==1){
                return show(202,'该房间/桌子正在使用中，不可删除');
            }
            $shop = Shop::where('user_id',$user_id)
                ->where('id',$ShopRoomTable->shop_id)
                ->first();
            if(!$shop){
                return show(204,'该房间/桌子不属于该商家');
            }
            $data = ShopRoomTable::where('id',$id)->delete();
            if(!$data)
            {
                return show(201,'删除房桌失败');
            }
            return show(200,'删除房桌成功');
        }catch (\Exception $e){
            dd($e->getMessage());
            return show(201,'删除房桌失败');
        }

    }

    //房桌状态
    public function showRoomTable(Request $request)
    {
        $rules = [
            'shop_id' => 'required|integer|exists:shops,id',
        ];
        $message = [
            'shop_id.*'=>'商家信息有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        $now_role = $this->thisLoginClerk->role;
        if ($now_role == '-1'){         // 商家
            // 判断这个商铺是否属于该商家
            $user_id = $this->thisLoginClerk->id;
            if (!Shop::checkIsShop($request->shop_id,$user_id)){
                return show(2002,'没有权限');
            }
        }else{// 服务员
            // 判断这个商铺是否属于当前服务员的商铺
            if( $request->shop_id != $this->thisLoginClerk->shop_id){
                return show(2002,'没有权限');
            }
        }
        $data = ShopRoomTable::where('shop_id',$request->shop_id)->get();
        if($data->isEmpty())
        {
            return show(201,'房桌数据为空');
        }
        foreach ($data as $dt){
            if (strpos($dt->thumb,';')){
                $res = explode(';',$dt->thumb);
                $dt->thumb=$res[0];
            }
        }
        return show(200,'房桌数据获取成功',$data);
    }

    public function showOneRoomTable(Request $request)
    {
        $rules = [
            'id' => 'required|integer|exists:shop_room_tables,id',
        ];
        $message = [
            'id.*'=>'请选择正确餐桌'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(2003,$validator->errors()->first());
        }
        $id = $request->id;
        try{
            $data = ShopRoomTable::select(['id','name','type','note','hold','is_use','min_consumption','lock_qrcode','thumb','created_at','note'])->find($id);
            if(!$data)
            {
                return show(201,'房桌数据为空');
            }
        }catch (\Exception $e){
            return show(201,'房桌数据为空');
        }
        return show(200,'房桌数据获取成功',$data);
    }


}
